<?php

class Login extends Controller {

	var $uid;
	var $tpl;
	
	function setVars()
	{
		$this->uid = (object) $this->session->userdata('uid');
		//$this->tpl['tpljs'][]  = '';
		$this->tpl['jsOnLoad']  = '';
		$this->tpl['title'] = 'Login';
		$this->tpl['head'] = '';
		$this->tpl['menu'] = $this->Menu();
		$this->tpl['body'] = '';
	}
	
	function Login()
	{
		parent::Controller();
		$this->setVars();
		$this->load->library('account');
		log_message('debug', "Login Controler Class Initialized");
		//$this->output->enable_profiler(TRUE);
	}

	function Menu()
	{
		$menu[] = anchor('/','Home');
		$menu[] = anchor('account/register/','Register');
		return $menu;
	}

/*****************************************************************\
|               LOGIN
\*****************************************************************/	
	function Index()
	{
		if(isset($_POST['Alias']))
		{
			$this->postCheck();
		}
		else
		{
			$this->load->view('account/login', $this->tpl);
		}
	}
	function postCheck()
	{
		log_message('debug', "AuthTaMeR Class Post Login received");
		$this->load->library('validation');
		$rules['Alias']	= "trim|xss_clean|required|alpha_numeric";
		$rules['Password']	= "trim|xss_clean|required|alpha_numeric";
		$this->validation->set_rules($rules);
		$fields['Alias'] = 'Alias';
		$fields['Password'] = 'Password';
		$this->validation->set_fields($fields);
		
		if ($this->validation->run() == TRUE)
		{
			$rules['login'] = "callback_login_check"; # Calling login check
			$this->validation->set_rules($rules) ;
			log_message('debug', "AuthTaMeR Class Post if 2");
			
			if ($this->validation->run() == FALSE)
			{
				log_message('debug', "AuthTaMeR Class Post if 3");
			}

		}
		log_message('debug', "AuthTaMeR Class Post end");
		//$this->msg->getMsg();
		$this->load->view('account/login',$this->tpl);
	}

	/***************************************************\
	*	If post is correct we will login
	\***************************************************/
	function login_check($str)
	{
		log_message('debug', "AuthTaMeR Class check_Login");
		$alias = $this->validation->Alias;
		$pass  = $this->validation->Password;
		$save  = $this->input->post('save');
		$this->load->model('pdb');
		$data = $this->pdb->Login($alias,$pass);
		if ($data)
		{
			//100 = fresh login not from cookie
			$data->groups .= ',100';
			$this->session->set_userdata($data);
 			if($save == 1)
			{
				$this->auth->addCookie();
			}
			redirect('account', 'refresh');
		}
		else
		{
			$this->validation->set_message('login_check', 'We have no match');
			return FALSE;
		}
	}

	/*****************************************************************\
	|               LOGOUT
	\*****************************************************************/

	function LogOut()
	{
		$this->auth->Logout();
	}
}
?>
